package com.tuju.currency.base

import android.graphics.drawable.AnimationDrawable
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.ViewStub
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.bumptech.glide.Glide
import com.tuju.currency.R
import com.tuju.currency.utils.PerfectClickListener
import rx.Subscription
import rx.subscriptions.CompositeSubscription

abstract class BaseFragment : Fragment() {

    protected var mIsVisible = false
    lateinit var contentView: View

    lateinit var mContainer: RelativeLayout

    // 加载中
    lateinit var loadingView: View
    // 加载失败
    lateinit var errorView: View
    //空布局
    lateinit var emptyView: View
    lateinit var mAnimationDrawable: AnimationDrawable

    var mCompositeSubscription: CompositeSubscription? = null


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val ll = inflater.inflate(R.layout.fragment_base, null)
        contentView = LayoutInflater.from(context).inflate(setContent(), null, false)
        val params = RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
        )
        contentView.layoutParams = params
        mContainer = ll.findViewById(R.id.container)
        mContainer.addView(contentView)
        return ll
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        loadingView = (getView(R.id.vs_loading) as ViewStub).inflate()
        emptyView = (getView(R.id.vs_empty) as ViewStub).inflate()//空布局
        errorView = (getView(R.id.vs_error) as ViewStub).inflate()
        val img = loadingView.findViewById<ImageView>(R.id.img_progress)
        mAnimationDrawable = img.drawable as AnimationDrawable
        // 默认进入页面就开启动画
        if (!mAnimationDrawable.isRunning) {
            mAnimationDrawable.start()
        }

        // 点击加载失败布局
        errorView.setOnClickListener(object : PerfectClickListener() {
            override fun onNoDoubleClick(v: View) {
                showLoading()
                onRefresh()
            }
        })
        emptyView.setOnClickListener(object : PerfectClickListener() {
            override fun onNoDoubleClick(v: View) {
                showLoading()
                onRefresh()
            }
        })
        contentView.visibility = View.GONE
        errorView.visibility = View.GONE
        emptyView.visibility = View.GONE
    }

    abstract fun setContent(): Int

    /**
     * 加载失败后点击后的操作
     */
    open fun onRefresh() {}

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        Log.i("yanze_fragment", "setUserVisibleHint: $isVisibleToUser  方法：  $userVisibleHint")
        if (userVisibleHint) {
            mIsVisible = true
            onVisible()
        } else {
            mIsVisible = false
            onInvisible()
        }
    }

    //不可见
    open fun onInvisible() {}

    open fun loadData() {}

    //可见
    open fun onVisible() {
        loadData()
    }

    /**
     * 显示加载中状态
     */
    protected fun showLoading() {
        if (loadingView != null && loadingView.visibility != View.VISIBLE) {
            loadingView.visibility = View.VISIBLE
        }
        // 开始动画
        if (!mAnimationDrawable.isRunning) {
            mAnimationDrawable.start()
        }
        if (contentView.visibility != View.GONE) {
            contentView.visibility = View.GONE
        }
        if (errorView.visibility != View.GONE) {
            errorView.visibility = View.GONE
        }
        if (emptyView.visibility != View.GONE) {
            emptyView.visibility = View.GONE
        }
    }

    /**
     * 加载完成的状态
     */
    protected fun showContentView() {
        if (loadingView.visibility != View.GONE) {
            loadingView.visibility = View.GONE
        }
        // 停止动画
        if (mAnimationDrawable.isRunning) {
            mAnimationDrawable.stop()
        }
        if (errorView.visibility != View.GONE) {
            errorView.visibility = View.GONE
        }
        if (contentView.visibility != View.VISIBLE) {
            contentView.visibility = View.VISIBLE
        }
        if (emptyView.visibility != View.GONE) {
            emptyView.visibility = View.GONE
        }
    }

    /**
     * 加载失败
     */
    protected fun showError() {
        if (loadingView != null && loadingView.visibility != View.GONE) {
            loadingView.visibility = View.GONE
        }
        // 停止动画
        if (mAnimationDrawable.isRunning) {
            mAnimationDrawable.stop()
        }
        if (errorView.visibility != View.VISIBLE) {
            errorView.visibility = View.VISIBLE
        }
        if (contentView.visibility != View.GONE) {
            contentView.visibility = View.GONE
        }
        if (emptyView.visibility != View.GONE) {
            emptyView.visibility = View.GONE
        }
    }

    /**
     * 空布局
     */
    protected fun showEmpty() {
        if (loadingView != null && loadingView.visibility != View.GONE) {
            loadingView.visibility = View.GONE
        }
        // 停止动画
        if (mAnimationDrawable.isRunning) {
            mAnimationDrawable.stop()
        }
        if (errorView.visibility != View.GONE) {
            errorView.visibility = View.GONE
        }
        if (contentView.visibility != View.GONE) {
            contentView.visibility = View.GONE
        }
        if (emptyView.visibility != View.VISIBLE) {
            emptyView.visibility = View.VISIBLE
        }
    }

    //设置错误界面图片n
    fun setErrorViewImg(resourId: Int) {
        if (errorView != null) {
            val errImg = errorView.findViewById<ImageView>(R.id.iv_error)
            Glide.with(this).load(resourId).into(errImg)
        }
    }

    //设置错误界面提示
    fun setErrorViewTips(tips: String) {
        if (errorView != null) {
            val errTips = errorView.findViewById<TextView>(R.id.tv_error_tips)
            errTips.setText(tips)
        }
    }

    //设置空界面图片
    fun setEmptyViewImg(resourId: Int) {
        if (emptyView != null) {
            val emptyImg = emptyView.findViewById<ImageView>(R.id.iv_empty)
            Glide.with(this).load(resourId).into(emptyImg)
        }
    }

    //设置空界面提示
    fun setEmptyViewTips(tips: String) {
        if (emptyView != null) {
            val emptyTips = emptyView.findViewById<TextView>(R.id.tv_empty_tips)
            emptyTips.setText(tips)
        }
    }

    //设置空界面点击事件
    fun setEmptyViewOnclick(onclick: View.OnClickListener) {
        emptyView.setOnClickListener(onclick)
    }

    protected fun <T : View> getView(id: Int): T {
        return view?.findViewById<View>(id) as T
    }


    //订阅事件管理
    fun addSubscription(s: Subscription?) {
        if (this.mCompositeSubscription == null) {
            this.mCompositeSubscription = CompositeSubscription()
        }
        this.mCompositeSubscription?.add(s)
    }

    fun removeSubscription() {
        this.mCompositeSubscription?.unsubscribe()
    }

    override fun onDestroy() {
        super.onDestroy()
        this.mCompositeSubscription?.unsubscribe()
    }
}